                                                                     
                                                                     
                                                                     
                                             
import japa.parser.JavaParser;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.body.FieldDeclaration;
import japa.parser.ast.body.MethodDeclaration;
import japa.parser.ast.body.VariableDeclarator;
import japa.parser.ast.expr.NameExpr;
import japa.parser.ast.visitor.VoidVisitorAdapter;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
public  class SingleClassParser{	
	static BufferedWriter methodFile;	
	static BufferedWriter memberFile;
    static BufferedWriter accessesFile;
	static boolean insideConstructor=false;
	static boolean insideMethod=false;
	@SuppressWarnings("unchecked")
	public void parseSingleClass(String fileName) throws Exception {		    
	    FileInputStream in = new FileInputStream(fileName);
	    memberFile = new BufferedWriter(new FileWriter("InfoFiles/Members.txt"));
        methodFile = new BufferedWriter(new FileWriter("InfoFiles/Methods.txt"));        
        accessesFile = new BufferedWriter(new FileWriter("InfoFiles/Accesses.txt"));
        
        CompilationUnit cu = null; 
        try {
        	cu = JavaParser.parse(in);
        	
        }
        catch(Exception e){
        	e.printStackTrace();
        }
        finally { 
            in.close();            
        }
    	//new ConstructorVisitor().visit(cu, null);
        new MethodVisitor().visit(cu, null);
        new FieldAccessExprVisitor().visit(cu, null);
        methodFile.close();
        memberFile.close();       
        accessesFile.close();
    } 
	private static class MethodVisitor extends VoidVisitorAdapter<Object> { 
	        public void visit(MethodDeclaration n, Object arg) {           
	        	
	        	try {	
	        		insideMethod=true;
	        		accessesFile.write("Start of Method");	 
	        		accessesFile.newLine();
					accessesFile.write(n.getName());					
					methodFile.write(n.getName());
					methodFile.newLine();					
					accessesFile.newLine();
					if(n.getBody()!=null)
					visit(n.getBody(), arg);
		            accessesFile.write("End of Method");
		            accessesFile.newLine();
		            insideMethod=false;
	        		}
	        	catch (IOException e) {		
	        		
					e.printStackTrace();
				}
	        } 
	        public void visit(NameExpr ne, Object o){
	        	try {
	        		if(insideMethod==true){
						accessesFile.write(ne.getName());						
						accessesFile.newLine();
		        	}
				} catch (IOException e) {					
					e.printStackTrace();
				}
	        } 
	    } 
	   
	  @SuppressWarnings("unchecked")
	private static class FieldAccessExprVisitor extends VoidVisitorAdapter { 
			 
			public void visit(FieldDeclaration n, Object arg) { 
		    
			    List<VariableDeclarator> aa = n.getVariables();
		        try {		        	 
		        	    String fieldName=aa.get(0).toString();
		        	    
		        	    if(fieldName.indexOf(" ")>=0)
		        	    {   
		        	    	memberFile.write(fieldName.substring(0, fieldName.indexOf(" ")));
		        	    }
		        	    else if(fieldName.indexOf("=")>=0)
		        	    {
		        	    	memberFile.write(fieldName.substring(0, fieldName.indexOf("=")));
		        	    }
		        	    else{
		        	    	memberFile.write(fieldName);
		        	    }
						memberFile.newLine();
				}
		        catch (IOException e) {				
					e.printStackTrace();
				}
		    } 
		} 
	  	 
}

